package com.effectiv.gooruda.rest.service.api;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.effectiv.gooruda.rest.domain.Cache;
import com.effectiv.gooruda.rest.domain.CacheEntry;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.core.IMap;

@Slf4j
@Path("/cache")
public abstract class CacheService {
	
	@Autowired
	@Qualifier("client")
	private HazelcastClient client;
	
	protected IMap<String, CacheEntry> getMap(String cacheName){
		String appCacheName = "" + "." + cacheName;
		
		IMap<String, CacheEntry> map = client.getMap(appCacheName);
		log.debug("Got map = {}", map);
		
		
		return map;
	}
	
	@Path("/{cacheName}")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public abstract Cache findOne(@PathParam("cacheName") String cacheName);
	
	@Path("/{cacheName}/key/{key}")
	@POST
	public abstract void addEntry(@PathParam("cacheName") String cacheName, @PathParam("key") String key, CacheEntry entry);
	
	@Path("/{cacheName}/key/{key}")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public abstract CacheEntry getEntry(@PathParam("cacheName") String cacheName, @PathParam("key") String key);
	
	@Path("/{cacheName}")
	@DELETE
	public abstract Cache delete(@PathParam("cacheName") String cacheName);
	
	@Path("/{cacheName}/clear")
	@POST
	public abstract void clear(@PathParam("cacheName") String cacheName);
	
	@Path("/{cacheName}/key/{key}/remove")
	@DELETE
	public abstract CacheEntry removeEntry(@PathParam("cacheName") String cacheName, @PathParam("key") String key);
	
	@Path("/{cacheName}/key/{key}/increment")
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	public abstract CacheEntry increment(@PathParam("cacheName") String cacheName, @PathParam("key") String key);

	// List<Cache> listCaches();
	// List<CacheEntry> findAll(String cacheName, String selector);
}
